package com.meitianhui.convenitenbuy.module.home.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.home.contract.ZeroBuyContract;
import com.meitianhui.convenitenbuy.module.home.http.HomeApiDataSource;
import com.meitianhui.convenitenbuy.module.home.model.bean.HomeGoodsInfo;
import com.meitianhui.convenitenbuy.module.home.ui.activity.ZeroBuyActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * 0元购（咚咚抢） presenter.
 * Created by Liam on 2018/5/17.
 */
public class ZeroBuyPresenter implements ZeroBuyContract.Presenter {

    private ZeroBuyActivity context;
    private ZeroBuyContract.View view;

    public ZeroBuyPresenter(ZeroBuyActivity context) {
        this.context = context;
        this.view = context;
    }

    @Override
    public void getZeroBuyGoodsList(String pageIndex, String pageSize) {
        Gson gson = new Gson();
        Map<String, String> pageMap = new HashMap<>();
        pageMap.put("page_no", pageIndex);
        pageMap.put("page_size", pageSize);
        String pageParams = gson.toJson(pageMap);

        Map<String, String> paramsMap = new HashMap<>();
        String params = gson.toJson(paramsMap);

        HomeApiDataSource.getInstance()
                .getEdinburgh(InfoProvider.getAppToken(App.getInstance()),
                        "psGoods.consumer.gdFreegetGoodsListPageFind", pageParams, params)
                .subscribe(new ResponseObserver<BaseResponse<HomeGoodsInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<HomeGoodsInfo> response) {
                        view.getZeroBuyGoodsListSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.showError(msg);
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
